<?php
session_start ();
$_SESSION ['pathway'] = '.';
$_SESSION ['pathway_js'] = '/fichespataxeurospa';

// ~ print_r($_POST);
// ~ print_r($_SESSION);

$date = getdate ();
$_SESSION ['date'] = $date ['year'] . '-' . $date ['mon'] . '-' . $date ['mday'];

if (! isset ( $_SESSION ['id'] )) {
	header ( 'Location: http://localhost/SPATAX_beta/index.php' );
} else {
	
	$_SESSION ['pathway'] = '.';
	$_SESSION ['pathway_js'] = '';
	require_once ("./requetes/last_login.php");
	require_once ("./commun/ouvre_base.php");
	include ("./header.php");
	
	?>
<form action="./export.php" method='post'>
	<table class='principale'>
		<tr>
			<td>Select table : <select name="table_to_export" id="statut">
					<option></option>
					<option value='diagnostic'
						<?php if(isset($_SESSION['table_to_export']) and $_SESSION['table_to_export'] == 'diagnostic'){ echo "selected='selected'";} ?>>
						Diagnosis</option>
					<option value='fiche_activite'
						<?php if(isset($_SESSION['table_to_export']) and $_SESSION['table_to_export'] == 'fiche_activite'){ echo "selected='selected'";} ?>>
						Activity form</option>
					<option value=''></option>
					<option value='linkage'
						<?php if(isset($_SESSION['table_to_export']) and $_SESSION['table_to_export'] == 'linkage'){ echo "selected='selected'"; } ?>>
						Linkage</option>
					<option value='wholegenomeanalysis'
						<?php if(isset($_SESSION['table_to_export']) and $_SESSION['table_to_export'] == 'wholegenomeanalysis'){ echo "selected='selected'"; } ?>>
						Whole Genome Analysis</option>
					<option value='screening'
						<?php if(isset($_SESSION['table_to_export']) and $_SESSION['table_to_export'] == 'screening'){ echo "selected='selected'"; } ?>>
						Screening</option>
			</select><input type='submit' value='Send' /></td>
		</tr>
	
	<?php
	
	$export_type = '';
	
	if (isset ( $_POST ['table_to_export'] ) and $_POST ['table_to_export'] != '') {
		
		if (preg_match ( "/diagnostic|fiche_activite/", $_POST ['table_to_export'] ) and $_SESSION ['acces_clinique'] >= 1 or preg_match ( "/linkage|screening|wholegenomeanalysis/", $_POST ['table_to_export'] ) and $_SESSION ['acces_genetique'] >= 1) {
			
			$_SESSION ['table_to_export'] = $_POST ['table_to_export'];
			
			$export_type = $_POST ['table_to_export'];
			
			$nomFichier = "$export_type.csv"; // nom du fichier xls
			$fichier = fopen ( $_SESSION ['pathway'] . "/tmp/" . $nomFichier, "w" ); // ouverture et création du fichier
			@chmod ( $_SESSION ['pathway'] . "/tmp/" . $nomFichier, 0777 );
			
			$requete_nom_champ = "SELECT ID_index_nom_champ, champ, nom_champ
			FROM `" . $export_type . "_nom_champ`";
			// ~ $sql_nom_champ = mysqli_query($link,$requete_nom_champ);
			$sql_nom_champ = mysql_query ( $requete_nom_champ );
			
			$ligne = '';
			if (mysql_num_rows ( $sql_nom_champ ) > 0) {
				// ~ if(mysqli_num_rows($sql_nom_champ) > 0){
				while ( $ligne = mysql_fetch_assoc ( $sql_nom_champ ) ) {
					// ~ while($ligne = mysqli_fetch_assoc($sql_nom_champ)){
					foreach ( $ligne as $champ => $valeur ) {
						$hash_index_champ [$ligne ['ID_index_nom_champ']] = $ligne ['champ'];
						$hash_champ_nomchamp [$ligne ['champ']] = $ligne ['nom_champ'];
					}
				}
			}
			
			$requete = "SELECT *
			FROM `$export_type`";
			$sql = mysql_query ( $requete );
			// ~ $sql = mysqli_query($link,$requete);
			
			$i = 0;
			$ligne = '';
			$outputCsv = '"';
			ksort ( $hash_index_champ ); // tri sur la clé qui est le numéro donnant l'ordre des champs
			if (mysql_num_rows ( $sql ) > 0) {
				// ~ if(mysqli_num_rows($sql) > 0){
				$compteur = 0;
				while ( $ligne = mysql_fetch_assoc ( $sql ) ) {
					// ~ while($ligne = mysqli_fetch_assoc($sql)){
					if ($i == 0) { // Si c'est la 1er boucle, on affiche le nom des champs pour avoir un titre pour chaque colonne
						foreach ( $hash_index_champ as $index => $chp ) {
							$outputCsv .= $hash_champ_nomchamp [$chp] . '";"';
						}
						$i ++;
					} else {
						foreach ( $hash_index_champ as $index => $chp ) {
							$outputCsv .= mysql_real_escape_string ( $ligne [$chp] ) . '";"';
							// ~ $outputCsv .= mysqli_real_escape_string($link,$ligne[$chp]).'";"';
							$compteur ++;
						}
					}
					$outputCsv = rtrim ( $outputCsv, '"' );
					$outputCsv .= "\n\"";
				}
				fwrite ( $fichier, $outputCsv ); // on écrit le contenu
				fclose ( $fichier );
			} else {
				echo "<table class='principale'><tr><td>No $export_type data</td></tr></table>";
			}
			
			echo "<table class='principale'><tr><td><a type=\"application/octet-stream\" href=" . $_SESSION ['pathway'] . "/tmp/" . $nomFichier . " >Download CSV file</a> </td></tr></table>";
		} else {
			echo "<tr><td><important>You don't have the permission to download this data</important></td></tr></table>";
		}
		include ($_SESSION ['pathway'] . "/footer.php");
	}
	?>
	
	</table>
</form>

<?php



}








?>
